package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.ProdDto;
import com.buer.storage.entity.Prod;
import com.buer.storage.service.ProdService;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//装备信息管理
@RestController
@RequestMapping("/prod")
public class ProdController {
    @Resource
    private ProdService prodService;

    //增加装备
    @PostMapping("/add.do")
    public R add(@RequestBody ProdDto prodDto) {
        if (prodService.add(prodDto) > 0) {
            return R.setR(1, "添加装备成功", null);
        }
        return R.setR(0, "添加装备失败", null);
    }

    //删除装备
    @GetMapping("/delete.do")
    public R delete(int prodId) {
        if (prodService.delete(prodId) > 0) {
            return R.setR(1, "删除装备成功", null);
        }
        return R.setR(0, "删除装备失败", null);
    }

    //修改装备
    @PostMapping("/update.do")
    public R update(@RequestBody ProdDto prodDto){
        if (prodService.update(prodDto)>0){
            //同步修该
            prodService.synchronization(prodDto);

            return R.setR(1,"修改装备成功",null);
        }
        return R.setR(0,"修改装备失败",null);
    }

    //查询装备
    @GetMapping("/select.do")
    public List<ProdDto> select(){

        return prodService.select();
    }

    //通过名字或者编号查询
    @PostMapping("/selectByName")
    public JsonResult selectByName(@RequestBody Map<String,Object> params){
        PageInfo<Prod> entityList = prodService.selectByName(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    //通过装备分类查询
    @GetMapping("/selectByCategory.do")
    public List<ProdDto> selectByCategory(int categoryId){
        List<ProdDto> prod = prodService.selectByCategory(categoryId);
        return prod;
    }

    @PostMapping("/cardupload.do")
    public R upload(MultipartFile file){
        return prodService.upload(file);
    }

    @PostMapping("/findAllProd")
    public JsonResult findAllProd(@RequestBody Map<String,Object> Prod) {
        PageInfo<ProdDto> entityList = prodService.findAllProd(Prod);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1, result);
    }

}
